#!/usr/bin/env bash

cd `dirname $0`
cd bin ;
ulimit -c unlimited
while :
do
    startTime=$(date "+%d")
    while :
    do
       processCnt=` ps -ef | grep camSolveMain | wc | awk '{print $1}'`
       echo "processCnt log cout : $processCnt"
       if [ $processCnt -lt 2 ];then
          echo "NO PROCESS RUNNING STARTING:"
          time=$(date "+%Y-%m-%d-%H:%M:%S")
          LD_LIBRARY_PATH=.:../lib/:./depend/ ./camSolveMain ./sysconfig.yml 2>&1 | tee log-${time}.txt &
       fi
       timeNow=$(date "+%d")
       echo "WATCH DOG TIME: $timeNow $startTime"
       if [ $timeNow -ne $startTime ];then
          echo "KILLING : $timeNow $startTime"
          killall camSolveMain
          break ;
       fi
       sleep 5
    done

    count=`ls log-* | wc -w`
    while [ $count -gt 5 ]
    do
       echo "cleaning log cout : $count"
       logs=`ls log-*`
       for log in $logs
       do
           rm -f $log
           echo "cleaning $log"
           break
       done
       count=`ls log-* | wc -w`
    done
    echo "Hit [ CTRL+C ] to stop!"
    sleep 3
done

# for Test use : timedatectl set-ntp false ; sudo date -s MM/DD/YY
# for check dump point : gdb ./camSolveMain  core


